﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Text;
using System.Web.Script.Services;
using ESpilWebservicePrototype.ViewModels;

namespace ESpilWebservicePrototype
{
    // Start the service and browse to http://<machine_name>:<port>/Service1/help to view the service's generated help page
    // NOTE: By default, a new instance of the service is created for each call; change the InstanceContextMode to Single if you want
    // a single instance of the service to process all calls.	
    [ServiceContract]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    // NOTE: If the service is renamed, remember to update the global.asax.cs file
    public class ESpilService
    {
        // TODO: Implement the collection resource that will contain the SampleItem instances

        #region Start
        //[WebGet(UriTemplate = "", ResponseFormat=WebMessageFormat.Json)]
        //public List<SampleItem> GetCollection()
        //{
        //    // TODO: Replace the current implementation to return a collection of SampleItem instances
        //    return new List<SampleItem>() { new SampleItem() { Id = 1, StringValue = "Hello" } };
        //}

        //[WebInvoke(UriTemplate = "", Method = "POST")]
        //public SampleItem Create(SampleItem instance)
        //{
        //    // TODO: Add the new instance of SampleItem to the collection
        //    throw new NotImplementedException();
        //}

        //[WebGet(UriTemplate = "{id}")]
        //public SampleItem Get(string id)
        //{
        //    // TODO: Return the instance of SampleItem with the given id
        //    throw new NotImplementedException();
        //}

        //[WebInvoke(UriTemplate = "{id}", Method = "PUT")]
        //public SampleItem Update(string id, SampleItem instance)
        //{
        //    // TODO: Update the given instance of SampleItem in the collection
        //    throw new NotImplementedException();
        //}

        //[WebInvoke(UriTemplate = "{id}", Method = "DELETE")]
        //public void Delete(string id)
        //{
        //    // TODO: Remove the instance of SampleItem with the given id from the collection
        //    throw new NotImplementedException();
        //} 
        #endregion

        [WebInvoke(UriTemplate = "login?username={username}&password={password}", Method = "GET", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json)]
        public int LogIn(string username, string password)
        {
            UserService service = new UserService();
            return service.ValidateUser(username, password);
        }

        [WebInvoke(UriTemplate = "create?username={username}&password={password}&name={name}&lastName={lastName}&cLass={cLass}", Method = "GET", BodyStyle = WebMessageBodyStyle.Wrapped)]
        public bool CreateUser(string username, string password, string name, string lastName, string cLass)
        {
            UserService service = new UserService();
            return service.CreateUser(username, password, name, lastName, cLass);
        }

        [WebInvoke(UriTemplate = "highscore?userID={userID}&score={score}", Method = "GET", BodyStyle = WebMessageBodyStyle.Wrapped)]
        public bool AddHighscore(int userID, int score)
        {
            UserService service = new UserService();
            return service.CreateHighscore(userID, score);
        }

        [WebInvoke(UriTemplate = "highscores?count={count}", Method = "GET", BodyStyle = WebMessageBodyStyle.Wrapped)]
        public List<Highscore> GetHighscores(int count)
        {
            UserService service = new UserService();

            //Makes sure that no more than 50 is returned
            int cappedCount = count > 50 ? 50 : count;

            return service.GetTopHighscores(cappedCount);
        }

        [WebInvoke(UriTemplate = "request?userid={userid}", Method = "GET", BodyStyle = WebMessageBodyStyle.Wrapped)]
        public User GetUser(int userID)
        {
            UserService service = new UserService();
            return service.GetUser(userID);
        }

        [WebInvoke(UriTemplate = "edituser?userID={userID}&username={username}&password={password}&name={name}&lastName={lastName}&cLass={cLass}", Method = "GET", BodyStyle = WebMessageBodyStyle.Wrapped)]
        public bool EditUser(int userID, string username, string password, string name, string lastName, string cLass)
        {
            UserService service = new UserService();
            return service.EditUser(userID, username, password, name, lastName, cLass);
        }

        [WebInvoke(UriTemplate = "befrienduser?userID={userID}&friendID={friendID}", Method = "GET", BodyStyle = WebMessageBodyStyle.Wrapped)]
        public void BefriendUser(int userID, int friendID)
        {
            UserService service = new UserService();
            service.BefriendUser(userID, friendID);
        }

        [WebInvoke(UriTemplate = "acceptfriendinvite?userID={userID}&friendID={friendID}", Method = "GET", BodyStyle = WebMessageBodyStyle.Wrapped)]
        public void AcceptFriendInvite(int userID, int friendID)
        {
            UserService service = new UserService();
            service.AcceptFriendInvite(userID, friendID);
        }

        [WebInvoke(UriTemplate = "unfriend?userID={userID}&friendID={friendID}", Method = "GET", BodyStyle = WebMessageBodyStyle.Wrapped)]
        public void Unfriend(int userID, int friendID)
        {
            UserService service = new UserService();
            service.CancelFriendInvite(userID, friendID);
        }

        [WebInvoke(UriTemplate = "getpendinginvites?userID={userID}", Method = "GET", BodyStyle = WebMessageBodyStyle.Wrapped)]
        public List<FriendsViewModel> GetPendingInvites(int userID)
        {
            UserService service = new UserService();
            return service.GetPendingInvites(userID);
        }

        [WebInvoke(UriTemplate = "getAllFriends?userID={userID}", Method = "GET", BodyStyle = WebMessageBodyStyle.Wrapped)]
        public List<User> GetAllFriends(int userID)
        { 
            UserService service = new UserService();
            return service.GetAllFriends(userID);
        }

        [WebInvoke(UriTemplate = "isfriend?userID={userID}&friendID={friendID}", Method = "GET", BodyStyle = WebMessageBodyStyle.Wrapped)]
        public Boolean IsFriend(int userID, int friendID)
        {
            UserService service = new UserService();
            return service.IsFriend(userID, friendID);
        }

        [WebInvoke(UriTemplate = "getAllUsers?q={query}&userID={userID}", Method = "GET", BodyStyle = WebMessageBodyStyle.Wrapped)]
        public UserListItemViewModel[] GetAllUsers(string query, int userID)
        {
            UserService service = new UserService();
            return service.GetAllUsers(query, userID).ToArray();
        }
    }
}
